//
//  XMCameraTypeListViewController.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/4/24.
//  Copyright © 2018年 smallyou. All rights reserved.
//

//controller
#import "XMTypeListViewController.h"
#import "XMUserCreateViewController.h"

//view
#import "XMBodyPartTableViewCell.h"
#import "XMSurgeryTypeTableViewCell.h"
#import "XMSurgeryDetailTableViewCell.h"
#import "XMTypeMaterialListView.h"

//request

//model


@interface XMTypeListViewController ()
//下一步
@property (nonatomic,strong) UIButton * nextBtn;
//部位model
@property (nonatomic,strong) NSString * partStr;
//手术类型数组
@property (nonatomic,strong) NSMutableArray * SurgeryTypeArr;
//材料数组
@property (nonatomic,strong) NSMutableArray * materialsArr;
//数据model
@property (nonatomic,strong) XMTypeListModel * listModel;
//现在选择的第三分类
@property (nonatomic,assign) NSUInteger thirdClassifyNum;
//现在选择的第四分类
@property (nonatomic,assign) NSUInteger fourthClassifyNum;
//现在选择的第四分类
@property (nonatomic,strong) UITextView * remarkTextView;
//现在选择的第四分类
@property (nonatomic,copy) NSString * remarkStr;
@end

@implementation XMTypeListViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"项目选择";
    [[SDImageCache sharedImageCache]clearMemory];

}

-(void)setUI{
    
    self.tableView.refreshEnable = NO;
    self.tableView.loadmoreEnable = NO;
    self.tableView.backgroundColor = kBgColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.equalTo(@50);
    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.nextBtn.mas_top);
    }];
    
}

#pragma mark ---netWork
-(void)getData{
    [KLProgressHUD showWaitingHUD];
    
    if (_type == 1) {
        [AppNetwork album_V2GetProjectListWithsubCateId:self.createModel.subCateId completion:^(BOOL success, XMTypeListModel *model, AppError *error) {
            
            [KLProgressHUD hideHUDWithAnimated:YES];
            if (success) {
                if (model.cateViews.count>0) {
                    //设置第三分类默认选择第一个
                    XMTypeListClassifyModel * cateModel = model.cateViews[0];
                    cateModel.isSelected = YES;
                    if (cateModel.cateViews.count>0) {
                        //设置第四分类默认选择第一个
                        XMTypeListClassifyModel * subCateModel = cateModel.cateViews[0];
                        subCateModel.isSelected = YES;
                    }
                }
                
                self.listModel = model;
                self.thirdClassifyNum = 0;
                self.fourthClassifyNum = 0;
                [self.tableView reloadData];
            }else{
                [KLProgressHUD showErrorHUDWithMessage:[error errorMessage]];
            }
        }];
    }else{
        [AppNetwork album_V2GetBeautyHistorycompletion:^(BOOL success, XMTypeListModel *model, AppError *error) {
            [KLProgressHUD hideHUDWithAnimated:YES];
            if (success) {
                if (model.cateViews.count>0) {
                    //设置第三分类默认选择第一个
                    XMTypeListClassifyModel * cateModel = model.cateViews[0];
                    cateModel.isSelected = YES;
                    if (cateModel.cateViews.count>0) {
                        //设置第四分类默认选择第一个
                        XMTypeListClassifyModel * subCateModel = cateModel.cateViews[0];
                        subCateModel.isSelected = YES;
                    }
                }
                
                self.listModel = model;
                self.thirdClassifyNum = 0;
                self.fourthClassifyNum = 0;
                [self.tableView reloadData];
            }else{
                [KLProgressHUD showErrorHUDWithMessage:[error errorMessage]];
            }
        }];
    }

    
}

#pragma mark ---action
-(void)nextAction{
    
    if (self.type == 1) {
        
        NSMutableArray * thridIdsArr = [NSMutableArray array];
        NSMutableArray * fourthIdsArr = [NSMutableArray array];
        NSMutableArray * fifthIdsArr = [NSMutableArray array];

        for (XMTypeListClassifyModel * thirdmodel in self.listModel.cateViews) {
            if (thirdmodel.isSelected) {
                [thridIdsArr addObject:thirdmodel.id];
            }
            
            for (XMTypeListClassifyModel * fourthModel in thirdmodel.cateViews) {
                if (fourthModel.isSelected) {
                    [fourthIdsArr addObject:fourthModel.id];
                }
                
                
                for (XMTypeListClassifyModel * fifthModel in fourthModel.cateViews) {
                    if (fifthModel.isSelected) {
                        [fifthIdsArr addObject:fifthModel.id];
                    }
                }
            }
        }
        
        self.createModel.subSubCateId = [thridIdsArr componentsJoinedByString:@","];
        self.createModel.subSubSubCateId = [fourthIdsArr componentsJoinedByString:@","];
        self.createModel.subSubSubSubCateId = [fifthIdsArr componentsJoinedByString:@","];
        
        self.createModel.remark = self.remarkTextView.text;
        
        
        if (self.createModel.partyId.length) {
            
            [KLProgressHUD showWaitingHUD];
            MJWeakSelf
            [AppNetwork album_createUserDataWithModel:self.createModel completion:^(BOOL success, NSString *data, AppError *error) {
                [KLProgressHUD hideHUDWithAnimated:YES];
                if (success) {
                    [KLProgressHUD showSuccessHUDWithMessage:@"上传图片成功"];
                    [weakSelf.navigationController popToRootViewControllerAnimated:YES];
                }else{
                    [KLProgressHUD showErrorHUDWithMessage:[error errorMessage]];
                }
            }];
            
            
            
        }else {
            
            XMUserCreateViewController * userVC = [XMUserCreateViewController userCreateView];
            userVC.createModel = self.createModel;
            [self.navigationController pushViewController:userVC animated:YES];
            
        }
        

        
    }else{
        if (self.callbackBlock) {
            self.callbackBlock(self.listModel.cateViews,self.remarkTextView.text);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }

}


#pragma mark ----tableView----

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        

        if (KRealValue(210)<210) {
            return 210;
        }else{
            return KRealValue(210);

        }
    }else if(indexPath.section == 1){
        
        XMTypeListClassifyModel * model =self.listModel.cateViews[self.thirdClassifyNum];
        
        NSUInteger countNum = 0;
        if (model.cateViews.count%3) {
            countNum = (model.cateViews.count/3+1);
        }else{
            countNum = model.cateViews.count/3;
        }

        if (countNum == 0) {
            countNum = 1;
        }else if (countNum>3) {
            countNum = 3;
        }
        
        int oneCountHeight  = 0;
        if (countNum<3) {
            oneCountHeight = 56;
        }else{
           oneCountHeight = 67;
        }

        if(KRealValue(countNum*oneCountHeight)<countNum*oneCountHeight) {
            return countNum*oneCountHeight;
        }else{
            return KRealValue(countNum*oneCountHeight);
        }
        
    }else{
        return 200;

    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 10.f;
    }else{
        return 0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]init];
    view.backgroundColor = kBgColor;
    return view;
}



-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section != 2) {
        return 10.f;
    }else{
        return 0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]init];
    view.backgroundColor = kBgColor;
    return view;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //身体部位
    if (indexPath.section == 0) {
        XMBodyPartTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[XMBodyPartTableViewCell className]];
        if (!cell) {
            cell = [[XMBodyPartTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[XMBodyPartTableViewCell className]];
        }
        //设置身体部分数据源
        cell.listData = self.listModel.cateViews;
        
        //设置点击回调
        MJWeakSelf
        [cell setSelectItemBlock:^(NSInteger index, BOOL isSelected,BOOL showSubList) {
            
            if (showSubList) {
                weakSelf.thirdClassifyNum = index;
                [weakSelf.tableView reloadData];
            }else{
                if (isSelected) {
                    weakSelf.thirdClassifyNum = index;
                    
                    //选择第三级后 默认设置第一个
                    XMTypeListClassifyModel * thirdModel = weakSelf.listModel.cateViews[index];
                    if (thirdModel.cateViews.count>0) {
                        XMTypeListClassifyModel * fourthModel = thirdModel.cateViews[0];
                        fourthModel.isSelected = YES;
                    }
                    [weakSelf.tableView reloadData];
                }else{
                    
                    //当第三级变为未选时 下级全部变为未选
                    XMTypeListClassifyModel * thirdModel = weakSelf.listModel.cateViews[index];
                    
                    for (XMTypeListClassifyModel * fourthModel in thirdModel.cateViews) {
                        fourthModel.isSelected = NO;
                        
                        for (XMTypeListClassifyModel *fifthModel in fourthModel.cateViews) {
                            fifthModel.isSelected = NO;
                        }
                    }
                    
                    [weakSelf.tableView reloadSection:2 withRowAnimation:UITableViewRowAnimationNone];
                }
            }
            

        }];
        
        return cell;
        
    }else if ( indexPath.section == 1){
        XMSurgeryTypeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[XMSurgeryTypeTableViewCell className]];
        if (!cell) {
            cell = [[XMSurgeryTypeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[XMSurgeryTypeTableViewCell className]];
        }
        
        if (self.listModel.cateViews.count>0) {
            XMTypeListClassifyModel * classifyModel = self.listModel.cateViews[self.thirdClassifyNum];
            cell.listData = classifyModel.cateViews;
        }
        
        //设置点击回调
        MJWeakSelf
        [cell setSelectItemBlock:^(NSInteger index, BOOL isSelected) {
            
            if (isSelected) {
                //第四级已选状态
                XMTypeListClassifyModel * thirdModel = weakSelf.listModel.cateViews[weakSelf.thirdClassifyNum];
                XMTypeListClassifyModel * fourModel = thirdModel.cateViews[index];
                
                if (fourModel.cateViews.count>0) {
                    MJWeakSelf
                    [XMTypeMaterialListView showTypeMaterialListWithDataArr:fourModel.cateViews resultBlock:^(NSArray *listArray) {
                        fourModel.cateViews = listArray;
                        [weakSelf.tableView reloadSection:2 withRowAnimation:UITableViewRowAnimationNone];

                    }];
                }
                
                [weakSelf.tableView reloadSection:2 withRowAnimation:UITableViewRowAnimationNone];
            }else{
                
                //当第四级变为未选时 下级全部变为未选
                XMTypeListClassifyModel * thirdModel = weakSelf.listModel.cateViews[weakSelf.thirdClassifyNum];
                XMTypeListClassifyModel * fourModel = thirdModel.cateViews[index];
                for (XMTypeListClassifyModel * fifthModel in fourModel.cateViews) {
                    fifthModel.isSelected = NO;
                }
                
                [weakSelf.tableView reloadSection:2 withRowAnimation:UITableViewRowAnimationNone];
                
                
            }
        }];
        
        
        return cell;
    }else{
        
        XMSurgeryDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[XMSurgeryDetailTableViewCell className]];
        if (!cell) {
            cell = [[XMSurgeryDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[XMSurgeryDetailTableViewCell className]];
        }
        
        cell.showRemark = YES;
        cell.timeLabel.text = self.timeStr;
        
        cell.partLabel.text = @"部位:";
        cell.surgetyTypeLabel.text = @"项目位置:";
        cell.materialsLabel.text = @"材料:";
        
        self.remarkTextView = cell.remarkTextView;
        if (self.createModel) {
            cell.remarkTextView.text = self.createModel.remark;
        }else{
            cell.remarkTextView.text = self.remarkStr;
        }
        MJWeakSelf
        [cell setTextChangeBlock:^(NSString *changeText) {
            if (weakSelf.createModel) {
                weakSelf.createModel.remark = changeText;

            }else{
                weakSelf.remarkStr = changeText;
            }
        }];
        if (cell.remarkTextView.text.length>0) {
            cell.remarkPlaceHolder.hidden = YES;
        }else{
            cell.remarkPlaceHolder.hidden = NO;
        }

        //设置列表
        for (XMTypeListClassifyModel * thridModel in self.listModel.cateViews) {
            if (thridModel.isSelected) {
                cell.partLabel.text = [NSString stringWithFormat:@"%@ %@",cell.partLabel.text,thridModel.name];
            }
            
            for (XMTypeListClassifyModel * fourthModel in thridModel.cateViews) {
                if (fourthModel.isSelected) {
                    cell.surgetyTypeLabel.text = [NSString stringWithFormat:@"%@ %@",cell.surgetyTypeLabel.text,fourthModel.name];
                }
                
                for (XMTypeListClassifyModel * fifthModel in fourthModel.cateViews) {
                    if (fifthModel.isSelected) {
                        cell.materialsLabel.text = [NSString stringWithFormat:@"%@ %@",cell.materialsLabel.text,fifthModel.name];
                    }
                }
            }
        }

        return cell;
    }
    
}


#pragma mark ----getting&setting----
-(UIButton *)nextBtn{
    if (!_nextBtn) {
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextBtn setBackgroundColor:kMainBlueColor];
        [_nextBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        [_nextBtn addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_nextBtn];
    }
    return _nextBtn;
    
}

-(void)setCreateModel:(XMUserCreateModel *)createModel{
    _createModel = createModel;
    if (self.createModel.partyId.length) {
        [self.nextBtn setTitle:@"完成" forState:UIControlStateNormal];
    }else{
        [self.nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    }
}

-(void)setType:(NSInteger)type{
    _type = type;
    [self getData];
}

-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache]clearMemory];
}
@end
